<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of entity_admin
 *
 * @author artcak
 */
class program_model extends CI_model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    
    /* memasukkan program baru */
    public function insert_program($tahun_id,$program_nama)
    {
        $this->db->where('TAHUN_ID', $tahun_id);
        $tmp1 = $this->db->get('tahun');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'TAHUN_ID' => $tahun_id,
          'PROGRAM_NAMA' => $program_nama,
          'PROGRAM_AKTIF' => 1
        );
        $result = $this->db->insert('program',$data);
        return $result;
    }
    
    public function insert_program_tahun($tahun_nama)
    {
        $this->db->where('TAHUN_NAMA', $tahun_nama);
        $tmp1 = $this->db->get('tahun');
        if($tmp1->num_rows() > 0)
        {
            return FALSE;
        }
        $data = array('TAHUN_NAMA' => $tahun_nama);
        $result = $this->db->insert('tahun',$data);
        return $result;        
    }
    
    /* mengupdate program */
    public function update_program($program_id,$tahun_id, $program_nama)
    {
        $this->db->where('TAHUN_ID', $tahun_id);
        $tmp1 = $this->db->get('tahun');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('PROGRAM_ID', $program_id);
        $tmp2 = $this->db->get('program');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'TAHUN_ID' => $tahun_id,
          'PROGRAM_NAMA' => $program_nama,
          'PROGRAM_AKTIF' => 1
        );
        $this->db->where('PROGRAM_ID', $program_id);
        $result = $this->db->update('program',$data);        
        return $result;
    }
    
    /* Hapus program */
    public function delete_program($program_id)
    {
        $this->db->where('PROGRAM_ID', $program_id);
        $tmp2 = $this->db->get('program');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('PROGRAM_ID', $program_id);
        $result = $this->db->delete('program');        
        return $result;        
    }
    
    public function get_prog_from_id($prog_id)
    {
        $this->db->where('PROGRAM_ID',$prog_id);
        $query = $this->db->get('program');
        return $query;
    }
    
    public function get_program()
    {
        $this->db->where('program_aktif',1);
        $query = $this->db->get('program');
        return $query;
    }
    
    public function get_program_by_tahun($tahun_id)
    {
        $this->db->where('tahun_id',$tahun_id);
        $this->db->where('program_aktif',1);
        $query = $this->db->get('program');
        return $query;        
    }
    
    public function get_list_tahun()
    {
        $query = $this->db->get('TAHUN');
        return $query;
    }
    
    public function get_tahun_by_id($id_tahun)
    {
        $this->db->where('tahun_id',$id_tahun);
        $query = $this->db->get('TAHUN');
        return $query;        
    }
}?>
